package com.hainatong.shiphelper.module.workermgr;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ActivityWorkerInfoDetailBinding;

import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.holiday.entity.DictFunBean;
import com.hainatong.shiphelper.module.holiday.entity.VacationParam;
import com.hainatong.shiphelper.module.holiday.fragment.FireWorkerFragment;
import com.hainatong.shiphelper.module.holiday.viewmodel.VacationViewModel;
import com.hainatong.shiphelper.module.workermgr.entity.WorkerBean;
import com.hainatong.shiphelper.module.workermgr.fragment.BaseWorkerInfoFragment;
import com.hainatong.shiphelper.module.workermgr.fragment.WorkerSalaryInfoFragment;
import com.hainatong.shiphelper.utils.GlideUtil;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.PushToast;
import com.hainatong.shiphelper.widget.LoadingDialog;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;
import com.jeremyliao.liveeventbus.LiveEventBus;


import java.util.ArrayList;


public class WorkerInfoDetailActivity extends BaseMvvmActivity<ActivityWorkerInfoDetailBinding, VacationViewModel>
{
    public static String WORKER_INFO_TAG = "workerInfo";

    private WorkerBean mUserInfo;

    private LoadingDialog loading;

    public static void startViewByParam( Activity mContext, WorkerBean userInfo )
    {
        Bundle bundle = new Bundle();
        bundle.putString(WORKER_INFO_TAG, GsonUtil.obj2json( userInfo ));
        start(mContext, bundle, false);
    }

    public static void start(Activity mContext, Bundle bundle)
    {
        start(mContext, bundle, false);
    }

    public static void start(Activity mContext, Bundle bundle, boolean isFinish)
    {
        Intent intent = new Intent();
        intent.setClass(mContext, WorkerInfoDetailActivity.class);
        if (bundle != null) intent.putExtra(mContext.getPackageName(), bundle);
        mContext.startActivity(intent);
        if (isFinish)
        {
            mContext.finish();
        }
    }


    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_worker_info_detail;
    }

    @Override
    protected int getBindingVariable()
    {
        return 0;
    }

    public Bundle getBundle()
    {
        if (getIntent() != null && getIntent().hasExtra(getPackageName()))
            return getIntent().getBundleExtra(getPackageName());
        else
            return null;
    }

    @Override
    protected void initView()
    {
        Bundle bundle = getBundle();
        if(ObjectUtils.isNotEmpty( bundle ))
        {
            String infoStr = bundle.getString(WORKER_INFO_TAG ,"");
            if(!StringUtils.isEmpty( infoStr ))
            {
                mUserInfo = GsonUtil.json2Obj( infoStr ,WorkerBean.class );
            }
        }


//        PushToast.getInstance().init(this);
        setToolbarTitle(viewDataBinding.includeTitle.toolbar);
        setToolBar(viewDataBinding.includeTitle.toolbar);

        loading = new LoadingDialog( this ,getString( R.string.loading ) );
        if( ObjectUtils.isNotEmpty( mUserInfo ) )
        {
            viewDataBinding.tvWorkerName.setText( mUserInfo.getUserTrueName() );
            viewDataBinding.tvWorkerJob.setText( mUserInfo.getRoleName() );
            GlideUtil.displayCircle( viewDataBinding.ivUserHeader , mUserInfo.getHeadImageUrl() );

            ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
            fragmentList.add( BaseWorkerInfoFragment.newInstance( GsonUtil.obj2json( mUserInfo ) ));
            fragmentList.add( WorkerSalaryInfoFragment.newInstance(GsonUtil.obj2json(mUserInfo)));
            viewDataBinding.slidingTl.setViewPager(viewDataBinding.vp2,new String[]{ getString(R.string.base_info_tag) , getString(R.string.salary_info_tag) },
                    this, fragmentList);
        }


        //不能适岗
        viewDataBinding.btnCantJob.setOnClickListener( v -> {

            // 先去查询 不能试岗的 vacation  id
            loading.show();
            //查 休假类型  取出 不能 试岗
            viewModel.queryVacationTypeList();

            //PushToast.getInstance(this).show(PushToast.UPDATE_FAIL);

        } );
        // 更新 信息
        viewDataBinding.btnUpdateInfo.setOnClickListener( v -> {

            PushToast.getInstance(this).show( PushToast.UPDATE_SUCCESS );

        } );

    }

    @Override
    protected void initData()
    {
        // 查到 休假 类型 后 赛选
        viewModel.getVacationTypeResult().observe( this , result -> {
            loading.hide();
            if( result.isSuccess() )
            {
                if( result.getData() != null && !result.getData().isEmpty() )
                {
                    //赛选 不能 适岗
                    for(int i = 0;i < result.getData().size();i++ )
                    {
                        DictFunBean typeBean = result.getData().get(i);
                        if( typeBean.getDict_value().contains("不能适岗") )
                        {
                            //显示 不能 适岗
                            VacationParam vacParam = new VacationParam();
                            // 设置 申请休假 类型  不能试岗
                            vacParam.setVactiontype(typeBean.getDict_value());
                            //"a6e91f09-5d1c-4e04-b2c7-0ad39e1c7e9a"
                            vacParam.setVactiontype_id(typeBean.getUuid());
                            // 离职人信息 传过去
                            LiveEventBus.get(FireWorkerFragment.FIRE_USER_TAG,WorkerBean.class).post( mUserInfo );
                            ComFragmentActivity.startViewByParam( WorkerInfoDetailActivity.this,Constants.VIEW_NO_JOB, GsonUtil.obj2json( vacParam ));

                        }
                    }

                }
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );



    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    private void setToolbarTitle(Toolbar toolbar)
    {
        // toolbar.setBackgroundColor(ContextCompat.getColor(this,R.color.red));

        ((TextView)toolbar.findViewById(R.id.tv_title)).setText("");
        ((ImageButton)toolbar.findViewById(R.id.btn_back)).setImageResource(R.drawable.svg_back);
        toolbar.findViewById(R.id.btn_back).setOnClickListener(v -> {
            supportFinishAfterTransition();
        });
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        loading.dismiss();
    }
}
